package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.adapter.TuiGuangAdapter;
import com.kpx.app.bean.TGMainBean;
import com.kpx.app.bean.TGbean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.StringUtil;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/3/28.
 * 我的推广列表
 */

public class ActivityMyTGList extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.txt_tg_all)
    TextView txtTgAll;
    @BindView(R.id.txt_tg_num)
    TextView txtTgNum;
    @BindView(R.id.txt_tg_total)
    TextView txtTgTotal;
    @BindView(R.id.txt_tg_un)
    TextView txtTgUn;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    private boolean isRefresh;
    private int pageNow;
    private TuiGuangAdapter adapter;
    private List<TGbean> tglist = new ArrayList();
    private int totalPage;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_tg_list);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "我的推广");
        UIUtils.initRefreshView(outView);
        titleview.setRight("去邀请", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.gotoActivity(ActivityMyTGList.this, ActivityInvaliteFriend.class);
            }
        }, R.color.white);
        titleview.setTxtSize(0,12);
    }

    @Override
    public void initData() {
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNow = 1;
                getData();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isRefresh = false;
                if (pageNow >= totalPage) {
                    ToastUtil.getInstance().showToast(ActivityMyTGList.this, "没有更多了");
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            outView.stopLoadMore();
                        }
                    }, 600);
                } else {
                    pageNow++;
                    getData();// 获取数据
                }
            }
        });
        adapter = new TuiGuangAdapter(ActivityMyTGList.this, tglist, R.layout.item_tuiguang_show);
        lvShow.setAdapter(adapter);
        isRefresh = true;
        getData();
    }

    private void getData() {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("pageIndex", pageNow + "");
        map.put("pageSize", "20");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                } else {
                    outView.stopLoadMore();
                }
                if (result.getCode() == 0) {
                    TGMainBean b = (TGMainBean) result.getData();
                    if (b != null) {
                        if (isRefresh) {
                            setHeadData(b);
                        }
                        if (b.getMainList() != null) {
                            if (isRefresh) {
                                tglist.clear();
                                if (b.getMainList().size() == 0) {
                                    txtEmptyMention.setText("您还没有推荐好友哦\n快去推荐好友拿奖励吧~");
                                    txtEmptyMention.setOnClickListener(null);
                                    txtEmptyMention.setVisibility(View.VISIBLE);
                                } else {
                                    txtEmptyMention.setVisibility(View.GONE);
                                }
                            }
                            totalPage = result.getPage().getPageSize();
                            outView.setAutoLoadMore(true);
                            outView.setPullRefreshEnable(true);
                            outView.setPullLoadEnable(true);
                            tglist.addAll(b.getMainList());
                            adapter.setData(tglist);
                        } else {
                            txtEmptyMention.setText("您还没有推荐好友哦\n快去推荐好友拿奖励吧~");
                            txtEmptyMention.setOnClickListener(null);
                            txtEmptyMention.setVisibility(View.VISIBLE);
                        }
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityMyTGList.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                if (isRefresh) {
                    outView.stopRefresh();
                    txtEmptyMention.setVisibility(View.VISIBLE);
                    txtEmptyMention.setText("获取信息失败，点击重试");
                    txtEmptyMention.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            getData();// 获取数据
                        }
                    });
                } else {
                    outView.stopLoadMore();
                }
                ToastUtil.getInstance().showToast(ActivityMyTGList.this, error);
            }
        }, TGMainBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_GET_TG_LIST, true, map);
    }

    DecimalFormat df = new DecimalFormat("###0.00");

    private void setHeadData(TGMainBean b) {
        txtTgAll.setText(Html.fromHtml(toHtml(StringUtil.formatAmount(df.format(Double.valueOf(b.getTotalIncome())), 2))));
        txtTgNum.setText(b.getTotalUser() + "");
        txtTgUn.setText(b.getWillIncome() + "");
        txtTgTotal.setText(b.getHadIncome() + "");
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    private String toHtml(String money) {
        String[] arr = money.split("[.]");
        if (arr.length > 1) {
            return "<big>" + arr[0] + "</big>" + "<small>." + arr[1] + "</small>";
        } else {
            return "<big>" + money + "</big>" + "<small>.00</small>";
        }
    }
}
